package com.wb.mas.ui.help;

import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.wb.mas.BR;
import com.wb.mas.R;
import com.wb.mas.data.DemoRepository;
import com.wb.mas.entity.HelpEntity;
import com.wb.mas.entity.HelpResponse;
import com.wb.mas.ui.base.viewmodel.ToolbarViewModel;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Copyright (C) 2018,
 * OrderViewModel
 * <p>
 * Description
 * 帮助中心
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019-04-30, mwl, Create file
 */
public class HelpViewModel extends ToolbarViewModel {

    //邮箱
    public ObservableField<String> email = new ObservableField<>();

    //电话
    public ObservableField<String> phone = new ObservableField<>();


    public HelpViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);

    }

    //给RecyclerView添加ObservableList
    public ObservableList<HelpItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<HelpItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.recycle_help);

    @Override
    public void onCreate() {
        super.onCreate();
        getDatas();
        init();
    }

    private void init() {
        setTitleText(getString(R.string.help_title));
        setRightIconVisible(View.GONE);
    }

    /**
     * 拨打电话事件
     */
    public BindingCommand callOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + phone.get());
            intent.setData(data);
            getApplication().startActivity(intent);
        }
    });

    private void getDatas() {
        showDialog();
        addSubscribe(model.getHelpDatas(new HashMap<String, Object>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HelpResponse>() {
                    @Override
                    public void accept(HelpResponse helpResponse) throws Exception {
                        dismissDialog();
                        if (helpResponse != null && helpResponse.getData() != null) {
                            setData(helpResponse.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        KLog.w(throwable.getMessage());
                    }
                }));
    }

    private void setData(List<HelpEntity> entities) {
        for (HelpEntity helpEntity : entities) {
            if (HelpEntity.TYPE_NORMAL.equals(helpEntity.getStatus())) {
                HelpItemViewModel itemViewModel = new HelpItemViewModel(this, helpEntity);
                observableList.add(itemViewModel);
            } else if (HelpEntity.TYPE_PHONE.equals(helpEntity.getStatus())) {
                phone.set(helpEntity.getMessage());
            } else if (HelpEntity.TYPE_EMAIL.equals(helpEntity.getStatus())) {
                email.set(helpEntity.getMessage());
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
